package cn.edu.ctbu.datajpa2.Service.impl;

import cn.edu.ctbu.datajpa2.Repository.StudentRepository;
import cn.edu.ctbu.datajpa2.Service.StudentService;
import cn.edu.ctbu.datajpa2.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService {
    @Autowired
     StudentRepository studentRepository;

    public List<Student> findAll(){
        return studentRepository.findAll();

    }

     public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    public Page<Student> findAll(Example<Student> student,Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }

    public Student findById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }


    public List<Student> findByName(String name){
        return studentRepository.findByNameLike(name);
    }

    public List<Student> findByNameAndPassword(String name,String password){
        return studentRepository.findByNameAndPassword(name,password);
    }


    public Student insert(Student student) {
        return studentRepository.save(student);
    }

    public Student update(Student student) {
        return studentRepository.save(student);
    }
    public void delete(Student student) {
        studentRepository.save(student);
    }
    public void delete(Integer id){
        Student student=new Student();
        student.setId(id);
        studentRepository.delete(student);
    }

    @Override
    public void add(Student student) {

    }

    public Student getById(Integer id){
        Student student=studentRepository.findById(id).orElse(null);
        return student;
    }
}

